<template>
  <div class="address">
    <!-- 列表 -->
    <div class="list">
      <van-swipe-cell v-for='item in list' :key='item.id'>
        <div class="item">
          <div class="left">
            <div class="name">{{item.name}}</div>
            <div class="moren" v-if="item.is_default == 1">默认</div>
          </div>
          <div class="middle" @click="sendAddress(item)">
            <div class="tel">{{item.mobile}}</div>
            <div class="info">{{item.address}} {{item.address_detail}}</div>
          </div>
          <router-link class="right" tag="div" :to="`/my/address/addressDetail?id=${item.id}`"></router-link>
        </div>
        <template #right>
          <van-button square text="删除" type="danger" class="delete-button" @click="delEvents(item.id)" />
        </template>
      </van-swipe-cell>
    </div>
    <!-- 新建地址 -->
    <div class="bottom">
      <div class="newAddress" @click="newAdress">+新建地址</div>
    </div>
  </div>
</template>

<script>
import { getListAction, deleteAction } from "@/api/my";
export default {
  data() {
    return {
      list: [], // 地址列表
    };
  },
  // 计算
  computed: {},
  // 创建
  created() {
    this.init();
  },
  // 准备
  mounted() {},
  // 方法
  methods: {
    init() {
      // 获取收货地址列表
      getListAction({
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        this.list = res.data.data;
      });
    },
    // 新建地址
    newAdress() {
      // 跳转页面
      this.$router.push("/my/address/addressDetail");
    },
    // 删除
    delEvents(val) {
      // 删除收货地址
      deleteAction({
        id: val,
      }).then((res) => {
        this.init();
      });
    },
    // 选择地址
    sendAddress(val) {
      this.$store.commit("uptAddressInfo", val);
      this.$router.go(-1);
    },
  },
};
</script>

<style lang='scss' scoped>
.address {
  height: calc(100% - 67px);
  border-top: 1px solid transparent;
  background-color: #fff;
  overflow: auto;
  // 列表
  .list {
    height: 100%;
    padding: 0 10px;
    background-color: #fff;
    .item {
      padding: 15px 0;
      border-bottom: 1px solid #d9d9d9;
      background-color: #fff;
      display: flex;
      align-items: center;
      // 左
      .left {
        width: 50px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        .name {
          margin: 12px auto;
        }
        .moren {
          width: 30px;
          height: 15px;
          border: 1px solid #b4282d;
          margin: 5px auto 0;
          color: #b4282d;
          line-height: 15px;
        }
      }
      // 中
      .middle {
        padding: 0 10px;
        flex: 1;
        text-align: left;
        .tel {
          margin: 12px 0;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .info {
          margin: 2.5px 0 12px;
          color: #666;
          // 2行不换行
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
      }
      // 右
      .right {
        width: 25px;
        height: 25px;
        background: url("~@/assets/images/edit.png") no-repeat;
        background-size: 100% 100%;
        margin: 0 10px;
        z-index: 999;
      }
    }
    // 删除
    .delete-button {
      height: 100%;
    }
  }
  // 新建地址
  .bottom {
    position: fixed;
    left: 0;
    bottom: 0;
    width: calc(100% - 30px);
    height: 37px;
    padding: 15px;
    background-color: #fff;
    .newAddress {
      width: 90%;
      height: 35px;
      line-height: 35px;
      border: 1px solid #b4282d;
      color: #b4282d;
      margin: 0 auto;
    }
  }
}
</style>